﻿/**
* oTested - source + widget
* $Tested - resultDOM + widget
*/
function getAPITests()
{
	function UPDATE(oTested)
	{
		var oResult = new TestResult();
		var aMustVals = [{name:'Moscow',value:0},{name:'New York',value:1},{name:'Berlyn',value:2}];
		var aActualVals = [];

		var id = oTested.attr('id');
		var $Tested = $('#' + id);
		
		oTested.jqcmbx('update', aMustVals);

		$('[data-jqc="item"]', $Tested).each(function (index)
		{
			var item = { 
				name: $(this).text(),
				value: parseInt($(this).data('value').toString())
			};
			aActualVals.push(item);
		});
		
		oResult.title = 'API: UPDATE method';
		oResult.must = aMustVals;
		oResult.actual = aActualVals;
		return oResult;
	};

	function OPEN(oTested)
	{
		var oResult = new TestResult();
		var oMustVals = {
			visibility: 'visible',
			display: 'block'
		};
		var oActualVals = {};

		var id = oTested.attr('id');
		var $Tested = $('#' + id);

		oTested.jqcmbx('open');

		var $listWraper = $('[data-jqc="list-wraper"]', $Tested);
		oActualVals['visibility'] = $listWraper.css('visibility');
		oActualVals['display'] = $listWraper.css('display');

		oResult.title = 'API: OPEN method';
		oResult.must = oMustVals;
		oResult.actual = oActualVals;
		return oResult;
	};

	function CLOSE(oTested)
	{
		var oResult = new TestResult();
		var oMustVals = {
			display: 'none'
		};
		var oActualVals = {};

		var id = oTested.attr('id');
		var $Tested = $('#' + id);

		oTested.jqcmbx('close');

		var $listWraper = $('[data-jqc="list-wraper"]', $Tested);
		oActualVals['display'] = $listWraper.css('display');

		oResult.title = 'API: CLOSE method';
		oResult.must = oMustVals;
		oResult.actual = oActualVals;
		return oResult;
	};

	function focus(oTested)
	{
		var oResult = new TestResult();
		var oMustVals = 'focus fired';
		var oActualVals = '';

		var id = oTested.attr('id');
		var $Tested = $('#' + id);

		oTested.bind( 'jqcmbxfocus', function (event) {
			oTested.data('focus-result', oMustVals);
		} );
		
		$Tested.focus();

		oActualVals = oTested.data('focus-result');

		oResult.title = 'API: FOCUS event';
		oResult.must = oMustVals;
		oResult.actual = oActualVals;
		return oResult;
	};

	function blur(oTested)
	{
		var oResult = new TestResult();
		var oMustVals = 'blur fired';
		var oActualVals = '';

		var id = oTested.attr('id');
		var $Tested = $('#' + id);

		oTested.bind( 'jqcmbxblur', function (event) {
			oTested.data('blur-result', oMustVals);
		} );
		
		$Tested.focus();
		$Tested.blur();

		oActualVals = oTested.data('blur-result');

		oResult.title = 'API: BLUR event';
		oResult.must = oMustVals;
		oResult.actual = oActualVals;
		return oResult;
	};

	function onChange(oTested)
	{
		var oResult = new TestResult();
		var oMustVals = 1;
		var oActualVals = '';

		oTested.bind( 'jqcmbxchange', function (event) { 
			oTested.data('change-result', $(this).jqcmbx('selectedIndex') ); 
		} );
		oTested.jqcmbx('selectedIndex', oMustVals)
		oActualVals = oTested.data('change-result');

		oResult.title = 'API: onChange event';
		oResult.must = oMustVals;
		oResult.actual = oActualVals;
		return oResult;
	};

	function KeysEvents(oTested)
	{
		var oResult = new TestResult();
		var oMustVals = 0;
		var oActualVals = 0;
		oTested.bind( 'jqcmbxkeydown', function (event) { 
			//console.log('keydown', event.which);
		} );
		oTested.bind( 'jqcmbxkeyup', function (event) { 
			//console.log('keyup', event.which);
		} );
		oTested.bind( 'jqcmbxkeypress', function (event) { 
			//console.log('keypress', event.which);
		} );

		oResult.title = 'API: Keys events';
		oResult.must = oMustVals;
		oResult.actual = oActualVals;
		return oResult;
	};

	var aApiTests = new TestSiute();
	aApiTests.push(UPDATE);
	aApiTests.push(OPEN);
	aApiTests.push(CLOSE);
	aApiTests.push(focus);
	//aApiTests.push(blur);
	aApiTests.push(onChange);
	aApiTests.push(KeysEvents);

	return aApiTests;
}